package com.proton.carepatchtemp.ui.ChartView;

import android.content.Context;
import android.widget.TextView;

import com.github.mikephil.charting.components.MarkerView;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.utils.MPPointF;
import com.proton.carepatchtemp.R;
import com.proton.carepatchtemp.constant.Config;
import com.proton.carepatchtemp.constant.Extras;
import com.proton.carepatchtemp.constant.Settings;
import com.proton.carepatchtemp.utils.SpUtils;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;

/**
 * Custom implementation of the MarkerView.
 *
 * @author Philipp Jahoda
 */
public class XYMarkerView extends MarkerView {

    private TextView tvContent;

    private DecimalFormat format;

    private long startTime = 0;

    public XYMarkerView(Context context, IAxisValueFormatter xAxisValueFormatter) {
        super(context, R.layout.custom_marker_view);

        tvContent = findViewById(R.id.tvContent);
        format = new DecimalFormat("###.00");
    }

    // callbacks everytime the MarkerView is redrawn, can be used to update the
    // content (user-interface)
    @Override
    public void refreshContent(Entry e, Highlight highlight) {
//        if (25F < e.getY() && e.getY() < 42F) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long time = startTime + (int) e.getX() * Settings.TEMP_LOAD_TIME_DIV;

        String unit = "℃";
        if (1 == SpUtils.getInt(Extras.TEMP_UNIT, Config.TEMP_UNIT_DEFAULT)) {
            unit = "℃";
        } else {
            unit = "℉";
        }

        tvContent.setText(getContext().getString(R.string.string_time) + formatter.format(time).split(" ")[1] + "\n" + getContext().getString(R.string.string_temp) + format.format(e.getY()) + unit);
//            tvContent.setTextColor(ContextCompat.getColor(VCare.get(),R.color.blueCenter));
//        }
        super.refreshContent(e, highlight);
    }

    @Override
    public MPPointF getOffset() {
        return new MPPointF(-(getWidth() / 2), -getHeight());
    }

    public long getStartTime() {
        return startTime;
    }

    public void setStartTime(long startTime) {
        this.startTime = startTime;
    }
}
